﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using Rhino.Commons;
using Castle.Components.Validator;
using ValidationException = Rhino.Security.Exceptions.ValidationException;


namespace DomainModel.Services
{
    public class EntityService<T>: IEntityService<T>
    {
       

        public virtual T Get(int id)
        {
            return Repository<T>.Get(id);
        }

        public virtual ICollection<T> GetAll()
        {
            return Repository<T>.FindAll();
        }

        public virtual void Remove(T instance)
        {
            Repository<T>.Delete(instance);
        }

        public virtual void Save(T instance)
        {
            var validator = new ValidatorRunner(new CachedValidationRegistry());
            if (validator.IsValid(instance))
            {
                Repository<T>.Save(instance);
            }
            else
            {
                throw new ValidationException(validator.GetErrorSummary(instance));
            }
        }

        public bool IsUpdate(int id)
        {
            return Repository<T>.Get(id) != null;
        }

        
    }
}
